import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;


public class Robot extends MIDlet implements CommandListener 
{
 
  private Display display;
  private Command exitC;
  private Command inicioC, instruC, idiomaC, cambiopage;
   private Command derecha,izquierda, arriba, abajo, regresar,cblock, genmap, gogenmap;
  private CanvasForm  canvas1;
  private CanvasForm canvasmap;
  private Form page2;
  private TextField ingreso1; 
 private int  xr, yr, idiom;
 private int [][]  gmatrix; 
 private String iniciar, instrucciones, idioma,generars,salir,irmap;
 private String derechas, izquierdas, arribas, abajos, regresars, cblocks,genmaps;
 private String tcarscrip,ttext;
  
    public Robot()
    {   //inicializa la plantalla
        
        display = Display.getDisplay(this);
        canvas1 = new CanvasForm();
       display.setCurrent(canvas1); 
        canvas1.repaint(); 
        idiom = 0;
         eleidioma();   
        comandos();
    }
   public void eleidioma(){
       // 0 para espanol
       // 1 para ingles
   if(idiom == 0){
     iniciar = "Reiniciar";
     instrucciones = "Instrucciones";
     idioma = "Idioma";
     generars = "Generar Script";
     salir = "Salir";
     irmap ="Ir Gen. Map";
     derechas = "Derecha";
     izquierdas= "Izquierda";
     arribas ="Arriba";
     abajos ="Abajo";
     regresars= "Regresar";
     cblocks= "Colocar Bloque";
     genmaps= "Generar Mapa";
     tcarscrip= "Cargar Script";
     ttext = "Ingreso Programa";
     
             }
   if(idiom == 1){
     iniciar = "Reset";
     instrucciones = "Instructions";
     idioma = "Language";
     generars = "Generate Script";
     salir = "Exit";
     irmap ="Ir a Gen. Script";
     derechas = "Right";
     izquierdas= "Left";
     arribas ="Up";
     abajos ="Down";
     regresars= "Go MapWorld";
     cblocks= "Place block";
     genmaps= "Generate Map";
     tcarscrip= "Upload script";
     ttext = "Program Income";
     
             }
       
   
   
   }
    public void comandos()
    {
        inicioC = new Command(iniciar,Command.OK,1);
            canvas1.addCommand(inicioC);            
        instruC = new Command(instrucciones,Command.OK,2);
            canvas1.addCommand(instruC);
        idiomaC = new Command(idioma,Command.OK,3);
            canvas1.addCommand(idiomaC);            
        cambiopage = new Command(generars, Command.OK,4);
            canvas1.addCommand(cambiopage);
        exitC = new Command(salir,Command.EXIT,1); 
            canvas1.addCommand(exitC);
        gogenmap = new Command(irmap,Command.OK,5);
            canvas1.addCommand(gogenmap);
            
        canvas1.setCommandListener(this);        
    }
    
     public void comandospag2()
    {
        derecha = new Command(derechas,Command.OK,1);
            page2.addCommand(derecha);            
        izquierda = new Command(izquierdas,Command.OK,2);
            page2.addCommand(izquierda);
        arriba = new Command(arribas,Command.OK,3);
            page2.addCommand(arriba);            
        abajo = new Command(abajos, Command.OK,4);
            page2.addCommand(abajo);
        regresar = new Command(regresars,Command.OK,5); 
            page2.addCommand(regresar);
 
        page2.setCommandListener(this);        
    }
     public void comandosgenmap()
    {
        cblock = new Command(cblocks,Command.OK,1);
            canvasmap.addCommand(cblock);            
        regresar = new Command(regresars,Command.OK,2); 
            canvasmap.addCommand(regresar);
        genmap = new Command (genmaps, Command.OK,3);
            canvasmap.addCommand(genmap);
 
        canvasmap.setCommandListener(this);        
    }
     

 
  public void ingresoblock(){
     xr = canvasmap.getXr();
     yr = canvasmap.getYr();
     gmatrix[yr][xr] = 1;
     canvasmap.setMapmatrix(gmatrix);
     canvasmap.repaint();
     gmatrix = canvasmap.getMapmatrix();
 }
  public void startApp(){
      
  }

  public void pauseApp(){}

  public void destroyApp(boolean unconditional){}
  
  public void commandAction(Command c, Displayable d)
  {
        if(c == exitC)
        {//lectura del comando salir
            destroyApp(false);
            notifyDestroyed();
        }  
        
        if(c == inicioC)
        {//
            canvas1 = new CanvasForm();
                display.setCurrent(canvas1); 

            canvas1.repaint();  
            comandos();
        } 
           
        if (c == idiomaC){
            canvas1 = new CanvasForm();
            display.setCurrent(canvas1); 
            canvas1.repaint();
            if (idiom == 1)
                idiom = 0;
            else
                idiom = 1;
            
            eleidioma(); 
            comandos();
        }
         if(c == gogenmap)
        {//
          
            canvasmap = new CanvasForm();
            display.setCurrent(canvasmap); 
            canvasmap.repaint();
            gmatrix = canvasmap.getMapmatrix();
            comandosgenmap();
        }
        if(c == cambiopage)
        {//     
             ingreso1 = new TextField(ttext,"",30,TextField.NUMERIC);
         page2 = new Form(tcarscrip);
            page2.append(ingreso1);
             display.setCurrent(page2); 
            comandospag2();
        }  
         if(c == regresar)
        {//
            display.setCurrent(canvas1); 

            canvas1.repaint();  
        }  
          if(c == derecha)
        {//
             canvas1.movDerecha();
            canvas1.repaint();  
        }  
          if(c == izquierda)
        {//
             canvas1.movIzquierda();
            canvas1.repaint();  
        }    
           if(c == arriba)
        {//
             canvas1.movArriba();
            canvas1.repaint();  
        }    
                      
            if(c == abajo)
        {//
             canvas1.movAbajo();
            canvas1.repaint();  
        }  
                if(c == cblock)
        {//
            
             ingresoblock();
            canvasmap.repaint();  
        } 
                if(c == genmap)
        {//
            yr = canvasmap.getYr();
            xr = canvasmap.getXr();
            for (int fil = 0; fil<21;fil++){
        for (int col = 0; col <20; col ++){
                if (gmatrix [fil][col] == 3)
                    gmatrix[fil][col]= 0;
                    }
            }
            gmatrix[yr][xr]=2;
            canvasmap.setMapmatrix(gmatrix);
            canvas1.setYr(yr);
            canvas1.setXr(xr);
            canvas1.setMapmatrix(canvasmap.getMapmatrix());
            canvas1.repaint();  
        } 
  } 
    
}